package com.common.sdk.net.download.request.model;

import com.common.sdk.net.download.util.DownloadFilePathUtils;

import android.annotation.SuppressLint;

@SuppressLint("UseSparseArrays")
public class DownloadInfo extends AbsDownloadInfo {

    /**安装状态  没有安装*/
    public static final int BUILD_NUBUILD = 0;
    /**安装状态  已经安装*/
    public static final int BUILD_BUILDED = 1;
    /**静默下载*/
    public static final int DOWNLOAD_PRIORITY_5 = 5;
    /**正常下载*/
    public static final int DOWNLOAD_PRIORITY_10 = 10;
    
    /**主线来源*/
    public static final int DOWNLOAD_SOURCE_MAIN = 1001;
    /**游戏联运*/
    public static final int DOWNLOAD_SOURCE_PLAY = 1002;
    /**其他*/
    public static final int DOWNLOAD_SOURCE_OTHER = 1100;
    
    // 是否取消，取消后的不再继续下载
    private boolean canceled = false;

    public boolean isCanceled() {
        return canceled;
    }

    public void setCanceled(boolean canceled) {
        this.canceled = canceled;
    }

    public DownloadInfo(int downloadSource, long cateCode, String packageName, int versionCode, String downloadPath) {
        this(downloadSource,cateCode, packageName, versionCode, downloadPath, DOWNLOAD_PRIORITY_10);
    }

    public DownloadInfo(int downloadSource,long cateCode, String packageName, int versionCode, String downloadPath, int downloadPriority) {
        super(downloadPath);
        // if (packageName == null || versionCode == 0 || downloadPath == null
        // || downloadPriority == 0) {
        // throw new
        // NullPointerException("versionName ,versionCode, downloadPath, downloadPriority is not empty");
        // }
        this.packageName = packageName;
        this.versionCode = versionCode;
        this.downloadPriority = downloadPriority;
        this.cateCode = cateCode;
        this.downloadSource = downloadSource;
    }

    @Override
    public String getKey() {// TODO 还需要改规则
        if (key == null) {
            key = packageName + "_" + versionCode;
        }
        return key;
    }
    
    @Override
    public String getSuffixName() {
        return DOWNLOAD_FILE_EXT;
    }
    /**下载优先级*/
    private int downloadPriority;
    /**包名*/
    private String packageName = "";
    /**文件名称*/
    private String fileName = "";
    /**文件后缀名称*/
    private String suffixName = "";
    /**线程数量*/
    private int threadNum = 0;
    /**版本号*/
    private int versionCode;
    /**icon图片地址*/
    private String iconPicPath;
    /**是否安装*/
    private int isbuild = BUILD_NUBUILD;
    /**下载备用地址*/
    private String downloadPathTwo;
    /**是否是静默下载 取消， */
    private volatile boolean isSilenceDownloadPause;
    /**SD卡在满足不足40%&&剩余空间<=1G时能否下载*/
    private volatile boolean isSdEnableDownload;
    /**驱动统计使用*/
    private long cateCode;
    /**下载来源*/
    private long downloadSource;
    @Deprecated
    public String errorCode;

    @Override
    public int getType() {
        return APK_TYPE;
    }

    public long getDownloadSource() {
        return downloadSource;
    }

    public int getDownloadPriority() {
        return downloadPriority;
    }

    public void setDownloadPriority(int downloadPriority) {
        this.downloadPriority = downloadPriority;
    }

    public String getPackageName() {
        return packageName;
    }

    public int getVersionCode() {
        return versionCode;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
    
    public String getSuffixChildrenName() {
        return suffixName;
    }

    public void setSuffixName(String suffixName) {
        this.suffixName = suffixName;
    }

    public int getIsbuild() {
        return isbuild;
    }

    public void setIsbuild(int isbuild) {
        this.isbuild = isbuild;
    }

    public String getDownloadPathTwo() {
        return downloadPathTwo;
    }

    public void setDownloadPathTwo(String downloadPathTwo) {
        this.downloadPathTwo = downloadPathTwo;
    }

    public boolean isSilenceDownloadPause() {
        return isSilenceDownloadPause;
    }

    public void pauseSilenceDownload() {
        this.isSilenceDownloadPause = true;
    }

    public void unPauseSilenceDownload() {
        this.isSilenceDownloadPause = false;
    }

    public boolean isSdEnableDownload() {
        return isSdEnableDownload;
    }

    public void unEnableDownload() {
        this.isSdEnableDownload = false;
    }

    public void enableDownload() {
        this.isSdEnableDownload = true;
    }

    public long getCateCode() {
        return cateCode;
    }

    public void setCateCode(long cateCode) {
        this.cateCode = cateCode;
    }

    public int getThreadNum() {
        return threadNum;
    }

    public void setThreadNum(int threadNum) {
        this.threadNum = threadNum;
    }

    public String getIconPicPath() {
        return iconPicPath;
    }

    public void setIconPicPath(String iconPicPath) {
        this.iconPicPath = iconPicPath;
    }

    /**
     * 是否安装过
     * @return
     */
    public boolean isbuild() {
        if (isbuild == 0) {// 还需要加参数
            return false;
        }
        return true;
    }

    /**
     * SD卡下班是否有文件
     * @return 
     */
    public boolean isHasSDFile() {// TODO 需要改
        return DownloadFilePathUtils.isHasSDFile(getDownloadFilePath());
    }

    @Override
    public String toString() {
        return super.toString() + ", downloadPriority:" + downloadPriority + ", packageName:" + packageName
                + ", fileName:" + suffixName + ", versionCode:" + versionCode + ", isbuild:" + isbuild;
    }


}
